import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:myapp/api/Http.dart';
import 'package:myapp/api/Urls.dart';
import 'package:myapp/model/GoodsDetailModel.dart';
import 'package:myapp/model/GoodsPushModel.dart';
import 'package:myapp/util/Constant.dart';
import 'package:myapp/util/IGenericDataProvider.dart';
import 'package:myapp/util/Utils.dart';
import 'package:url_launcher/url_launcher.dart';

class GoodsDetailPage extends StatelessWidget {
  Goods_detailsBean itemData;

  @override
  Widget build(BuildContext context) {
    final data = IGenericDataProvider.of<GoodsDetailModel>(context);
    itemData = data.goodsDetailResponse.goodsDetails[0];
    return Scaffold(
      appBar: AppBar(
        title: Text("商品详情"),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          _buildBanner(),
          _buildHome(context),
          _buildButton(context),
        ],
      ),
    );
  }

  Widget _buildBanner() {
    return Container(
      height: 300,
      child: Swiper(
          //第三方的banner库：flutter_swiper
          itemBuilder: (BuildContext context, int index) {
            return Container(
              width: double.infinity,
              height: 300,
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.all(Radius.circular(20))),
              child: Image.network(
                itemData.goodsGalleryUrls[index],
                height: double.infinity,
                fit: BoxFit.fill,
              ),
            );
          },
          autoplay: true,
          loop: true,
          itemCount: itemData.goodsGalleryUrls.length,
          scale: 0.9,
          pagination: new SwiperPagination(
            builder: SwiperPagination.fraction,
            alignment: Alignment.bottomRight,
          )),
    );
  }

  /*
  服务标签: 4-送货入户并安装,5-送货入户,6-电子发票,9-坏果包赔,11-闪电退款,12-24小时发货,13-48小时发货,17-顺丰包邮,18-只换不修,19-全国联保,20-分期付款,24-极速退款,25-品质保障,26-缺重包退,27-当日发货,28-可定制化,29-预约配送,1000001-正品发票,1000002-送货入户并安装
   */
  Map<int, String> serviceTagsMap = {
    4: "送货入户并安装",
    5: "送货入户",
    6: "电子发票",
    9: "坏果包赔",
    11: "闪电退款",
    12: "24小时发货",
    13: "48小时发货",
    17: "顺丰包邮",
    18: "只换不修",
    19: "全国联保",
    20: "分期付款",
    24: "极速退款",
    25: "品质保障",
    26: "缺重包退",
    27: "当日发货",
    28: "可定制化",
    29: "预约配送",
    1000001: "正品发票",
    1000002: "送货入户并安装"
  };

  List<Widget> hotNameList(BuildContext context, var nameList) {
    List<Widget> widgetList = List();
    for (int i = 0; i < nameList.length; i++) {
      Widget widget = GestureDetector(
        child: Container(
          margin: const EdgeInsets.all(6),
          padding: const EdgeInsets.all(6),
          // 盒子样式
          decoration: new BoxDecoration(
              color: Colors.teal,
              //设置Border属性给容器添加边框
              borderRadius: BorderRadius.all(Radius.circular(10))),
          child: Text(
            serviceTagsMap[nameList[i]],
            style: TextStyle(color: Colors.white),
          ),
        ),
        onTap: () {},
      );
      widgetList.add(widget);
    }
    return widgetList;
  }

  void pushUrl(BuildContext context) {
    Map<String, dynamic> paraMap = Map();
    paraMap["p_id"] = Constant.pId;
    paraMap["goods_id_list"] = [itemData.goodsId].toString();
    paraMap["search_id"] = itemData.searchId;
    DioUtils.postHttp(Urls.GenerateUrl, parameters: paraMap, onSuccess: (data) {
      print(data);
      GoodsPushModel goodsPushModel = GoodsPushModel.fromMap(jsonDecode(data));
      _launchURL(goodsPushModel
          .goodsPromotionUrlGenerateResponse.goodsPromotionUrlList[0].url);
    }, onError: (err) {
      print(err);
    });
  }

  void _launchURL(String url) async {
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      print('不能访问');
    }
  }

  Widget _buildHome(context) {
    return
      Flexible(
//      padding: const EdgeInsets.all(8),
      child:
        ListView(
      children: <Widget>[
        Column(
          children: <Widget>[
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                Row(
                  children: <Widget>[
                    Text(
                      "￥",
                      style: TextStyle(fontSize: 12, color: Colors.red),
                    ),
                    Text(
                      Utils.formatNum((0.01 * itemData.minGroupPrice), 2),
                      style: TextStyle(fontSize: 18, color: Colors.red),
                    ),
                  ],
                ),
                Text("已拼" + itemData.salesTip + "件"),
              ],
            ),
            Text(
              itemData.goodsName,
              style: TextStyle(
                  fontSize: 14,
                  fontStyle: FontStyle.normal,
                  fontWeight: FontWeight.bold),
            ),
            Text(
              itemData.goodsDesc,
              style: TextStyle(fontSize: 10, fontStyle: FontStyle.normal),
            ),
            Container(
              padding: const EdgeInsets.all(10),
              margin: const EdgeInsets.only(top: 10),
              color: Colors.red,
              child: Text(
                "满" +
                    (0.01 * itemData.couponMinOrderAmount).toString() +
                    "元 可用优惠券" +
                    (0.01 * itemData.couponDiscount).toString() +
                    "元",
                style: TextStyle(color: Colors.white),
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: 10),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                children: <Widget>[
                  Row(
                    children: <Widget>[
                      Text(
                        "描述分:",
                        style: TextStyle(fontSize: 12, color: Colors.grey),
                      ),
                      Text(
                        itemData.descTxt,
                        style: TextStyle(fontSize: 12, color: Colors.grey),
                      )
                    ],
                  ),
                  Row(
                    children: <Widget>[
                      Text(
                        "服务分:",
                        style: TextStyle(fontSize: 12, color: Colors.grey),
                      ),
                      Text(
                        itemData.servTxt,
                        style: TextStyle(fontSize: 12, color: Colors.grey),
                      )
                    ],
                  ),
                  Row(
                    children: <Widget>[
                      Text(
                        "物流分:",
                        style: TextStyle(fontSize: 12, color: Colors.grey),
                      ),
                      Text(
                        itemData.lgstTxt,
                        style: TextStyle(fontSize: 12, color: Colors.grey),
                      )
                    ],
                  ),
                ],
              ),
            ),
            Container(
              margin: const EdgeInsets.only(top: 10),
              child: Row(
                children: <Widget>[
                  Icon(
                    Icons.home,
                    color: Colors.redAccent,
                  ),
                  Text(itemData.mallName),
                ],
              ),
            ),
            Container(
              child: Row(
                children: hotNameList(context, itemData.serviceTags),
              ),
            )
          ],
        ),
      ],
      ),
    );
  }

  Widget _buildButton(context) {
   return Container(
      alignment: Alignment.bottomCenter,
      margin: const EdgeInsets.only(left: 8, right: 8),
      width: double.infinity,
      child: MaterialButton(
        color: Colors.blueAccent,
        child: Text(
          "领券购买",
          style: TextStyle(color: Colors.white),
        ),
        minWidth: double.infinity,
        onPressed: () {
          //推广
          pushUrl(context);
        },
      ),
    );
  }
}
